package io.lckgroup.modules.lck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;
import io.lckgroup.modules.lck.dao.CustBaseDao;
import io.lckgroup.modules.lck.dao.CustomerInfoDao;
import io.lckgroup.modules.lck.entity.CustBaseEntity;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.service.CustomerInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;


@Service("customerInfoService")
public class CustomerInfoServiceImpl extends ServiceImpl<CustomerInfoDao, CustomerInfoEntity> implements CustomerInfoService {

    @Autowired
    private CustBaseDao custBaseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CustomerInfoEntity> page = this.page(
                new Query<CustomerInfoEntity>().getPage(params),
                new QueryWrapper<CustomerInfoEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 功能描述: <br>
     * 〈〉 使用openid查询当前用户是否存在
     *
     * @Param: [customerInfoEntity]
     * @Return: 不存在 null   存在 返回实体
     * @Author: huangrui
     * @Date: 2020/2/11 15:05
     */
    @Override
    public CustomerInfoEntity getCustomerByOpenid(String openid) {
        QueryWrapper<CustomerInfoEntity> customWrapper = new QueryWrapper<>();
        customWrapper.eq("open_id", openid);
        CustomerInfoEntity customerInfoEntity = baseMapper.selectOne(customWrapper);
        return customerInfoEntity;
    }

    /**
     * 功能描述: <br>
     * 〈〉保存当前custom信息，并且返回完整信息
     *
     * @Param: [customerInfoEntity]
     * @Return: 保存当前custom信息，并且返回完整信息,成功返回对象，失败返回null
     * @Author: huangrui
     * @Date: 2020/2/11 15:07
     */
    @Override
    public CustomerInfoEntity saveAndSelectCustomer(CustomerInfoEntity customer) {
        baseMapper.insert(customer);//插入用户数据
        QueryWrapper<CustomerInfoEntity> customWrapper = new QueryWrapper<>();
        customWrapper.setEntity(customer);
        CustomerInfoEntity CustomerInfoEntity = baseMapper.selectOne(customWrapper);//查询出该用户数据
        //插入base表基本数据
        CustBaseEntity entity = new CustBaseEntity();
        entity.setUserId(CustomerInfoEntity.getId());
        entity.setSex(CustomerInfoEntity.getSex().toString());
        entity.setUpdateDate(new Date());
        entity.setCreateDate(new Date());
        entity.setRole("1");
        custBaseDao.insert(entity);
        return CustomerInfoEntity;
    }


}